import dbl from "@/utils/dbl";
import { getDict } from "~/utils";
import {
    getUnit,
    validatorUnitNameByEdit,
    validatorcreditNumberByEdit,
    updateEntity
} from "@/views/authorization-management/apply-unit/api/apply-unit.js";
import uploadMedia from "~/components/upload-media.vue";

export default {
    components: {
        uploadMedia
    },
    data() {
        /**
         * async / await 同步
         * 校验申请单位的唯一性
         */
        let validatorUnitName = async (rule, value, callback) => {
            if (value == "") {
                callback(new Error("请输入单位名称"));
            }
            await validatorUnitNameByEdit(
                value,
                this.editObj.applyUnit.apUiId
            ).then(respData => {
                let result = respData.data.data;
                if (result == false) {
                    callback(new Error("单位名称重复"));
                }
            });
        };
        let validatorPhone = async (rule, value, callback) => {
            if (value == "") {
                callback(new Error("请输入联系人手机"));
            }
            const reg = /^1[0-9]{10}$/;
            if (!reg.test(value)) {
                callback(new Error("请输入合法的联系人手机"));
            }
        };

        /**
         * async / await 同步
         * 校验社会统一信用码唯一性
         */
        let validatorcreditNumber = async (rule, value, callback) => {
            if (value == "") {
                callback(new Error("请输入社会统一信用码"));
            }
            await validatorcreditNumberByEdit(
                value,
                this.editObj.applyUnit.apUiId
            ).then(respData => {
                let result = respData.data.data;
                if (result == false) {
                    callback(new Error("社会统一信用码重复"));
                }
            });
        };

        return {
            labelPosition: "top",
            loading: true,
            //弹出框 初始时隐藏
            dialogVisible: false,
            editObj: {
                applyUnit: {
                    apUiId: "",
                    unitName: "",
                    unitType: "",
                    userName: "",
                    tell: "",
                    creditNumber: "",
                    sort: "",
                    applyProperty: "",
                    remarks: ""
                },
                //附件
                fileList: []
            },
            rules: {
                applyUnit: {
                    unitName: [
                        {
                            required: true,
                            message: "请输入单位名称",
                            trigger: "blur"
                        },
                        { validator: validatorUnitName, trigger: "blur" },
                        { min: 2, max: 50, message: "单位名称长度必须小于50" }
                    ],
                    unitType: [
                        {
                            required: true,
                            message: "请选择单位类型",
                            trigger: "change"
                        }
                    ],
                    userName: [
                        {
                            required: true,
                            message: "请输入联系人姓名",
                            trigger: "blur"
                        },
                        { min: 2, max: 20, message: "联系人姓名必须小于20" }
                    ],
                    tell: [
                        {
                            required: true,
                            message: "请输入联系电话",
                            trigger: "blur"
                        },
                        { validator: validatorPhone, trigger: "blur" }
                    ],
                    creditNumber: [
                        {
                            required: true,
                            message: "请输入社会统一信用码",
                            trigger: "blur"
                        },
                        {
                            min: 18,
                            max: 18,
                            message: "社会统一信用代码长度为18位"
                        },
                        { validator: validatorcreditNumber, trigger: "blur" }
                    ],
                    sort: [
                        {
                            required: true,
                            message: "请输入排序号",
                            trigger: "blur"
                        }
                    ],
                    applyProperty: [
                        {
                            required: true,
                            message: "请输入物业",
                            trigger: "blur"
                        }
                    ]
                }
            }
        };
    },
    computed: {
        // 单位类型
        lists() {
            return getDict("apply-unit-type");
        }
    },
    methods: {
        //展示新增弹框
        show(rowData) {
            if (rowData != null) {
                //赋值
                const {
                    apUiId,
                    unitName,
                    unitType,
                    userName,
                    tell,
                    creditNumber,
                    sort,
                    applyProperty,
                    remarks
                } = rowData;
                this.editObj.applyUnit = {
                    apUiId,
                    unitName,
                    unitType,
                    userName,
                    tell,
                    creditNumber,
                    sort,
                    applyProperty,
                    remarks
                };
            }
            this.dialogVisible = true;
            this.loading = false;
        },

        /**
         * 保存按钮 触发文件上传
         */
        sendData() {
            this.$refs.editForm.validate(valid => {
                valid && this.$refs.unitUpdateFileUpload.submit();
            });
        },
        //保存表单信息
        editEntity() {
            //表单验证
            let editObj = this.editObj;
            updateEntity(editObj)
                .then(data => {
                    this.$message({
                        type: "success",
                        message: "操作成功"
                    });
                    //关闭弹窗
                    this.dialogVisible = false;
                    //关闭之后触发 列表页面的查询按钮的click事件
                    this.$emit("init");
                    //this.$parent.query();
                })
                .finally(() => (this.loading = false));
        }
    }
};
